package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.annotation.AutoFile;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.enumeration.OperationType;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface SetMealMapper {

    /**
     * 根据分类id查询套餐的数量
     * @param id
     * @return
     */
    @Select("select count(id) from setmeal where category_id = #{categoryId}")
    Integer countByCategoryId(Long id);

    /**
     * 根据ids查询套餐信息 用于删除菜品(dish)作为Exception的提示词
     * @param ids XML配置
     * @return name
     */
    List<String> getSetMealNameById(List<Long> ids);

    /**
     * 新增套餐
     * @param setmeal
     * @return
     */
    @AutoFile(value = OperationType.INSERT)
    @Insert("insert into setmeal (name, category_id, price, description, status, create_time, update_time, create_user, update_user,image) " +
            "values (#{name}, #{categoryId}, #{price}, #{description}, #{status}, #{createTime}, #{updateTime}, #{createUser}, #{updateUser},#{image})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Setmeal setmeal);

    /**
     * 套餐分页查询
     * XML配置
     * @param setmealPageQueryDTO
     * @return
     */
    Page<SetmealVO> queryPage(SetmealPageQueryDTO setmealPageQueryDTO);

    /**
     * 根据id查询套餐
     * XML配置
     * @param id
     * @return
     */
    SetmealVO getSetMealById(Long id);

    /**
     * 修改套餐
     * XML配置
     * @param setmeal
     * @return
     */
    @AutoFile(value = OperationType.UPDATE)
    int updateSetMeal(Setmeal setmeal);
    /**
     * 批量删除
     * @param ids
     * @return
     */
    int deleteSetMeal(List<Long> ids);
    /**
     * 动态条件查询套餐
     * @param setmeal
     * @return
     */
    List<Setmeal> list(Setmeal setmeal);

    /**
     * 根据套餐id查询菜品选项
     * @param setmealId
     * @return
     */
    @Select("select sd.name, sd.copies, d.image, d.description " +
            "from setmeal_dish sd left join dish d on sd.dish_id = d.id " +
            "where sd.setmeal_id = #{setmealId}")
    List<DishItemVO> getDishItemBySetmealId(Long setmealId);

    /**
     * 根据套餐的主键(id)查询他们的菜品分类id
     * @param ids
     * @return
     */
    List<Long> getCategoryIdBySetMealId(List<Long> ids);

    /**
     * 套餐批量更新状态
     * @param setmeal
     */
    @AutoFile(value = OperationType.UPDATE)
    @Update("update setmeal set status = #{status},update_time = #{updateTime},update_user = #{updateUser} where category_id = #{categoryId}")
    void updateStatusById(Setmeal setmeal);
    /**
     * 获取当前状态的套餐的名称!!
     * @param status
     * @param ids
     * @return
     */
    List<String> getStatusNamesByIds(List<Long> ids, Integer status);
    /**
     * 根据状态查询套餐状态数量
     * @param status
     * @return
     */
    @Select("select count(*) from setmeal where status=#{status}")
    Integer getSetmealStatusCountById(Integer status);
}
